<?php
declare (strict_types=1);

namespace app\model\admin;

use app\common\model\BaseModel;
use \libs\Log;

class PropMember extends BaseModel
{
    protected $pk = 'id';

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * @param $data
     * 赠送用户道具
     */
    public function giveToMember($data)
    {
        $where['prop_id'] = $data['prop_id'];
        $where['member_id'] = $data['member_id'];
        $propMember = $this->where($where)->field('prop_count')->find();
        if (empty($propMember)) {
            $propType = Prop::where('id', $data['prop_id'])->value('type');
            $data = [
                'member_id' => $data['member_id'],
                'prop_id' => $data['prop_id'],
                'prop_count' => $data['prop_count'],
                'prop_type' => $propType,
            ];
            self::create($data);
        } else {
            $totalCount = $propMember['prop_count'] + $data['prop_count'];
            $this->where($where)->save(['prop_count' => $totalCount]);
        }
        return $this->returnSucc();
    }
}
